﻿using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using S.Eartag.Infrastructure;
using S.Eartag.Infrastructure.Impl;
using S.Eartag.Infrastructure.Interface;
using System.Text;

namespace S.Eartag.Read.API.Extensions
{
    public static class ServiceCollectionExtensions
    {
        public static IServiceCollection AddServices(this IServiceCollection services)
        {

            services.AddTransient(typeof(IBaseRepository<>), typeof(BaseRepository<>));

            services.AddTransient<ILamBingRepository, LamBingRepository>();
            services.AddTransient<IMarkRepository, MarkRepository>();
            services.AddTransient<ISheepFileRepository, SheepFileRepository>();
            return services;
        }
        public static void AddJWT(this IServiceCollection collection, WebApplicationBuilder builder)
        {
            builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                .AddJwtBearer(options =>
                {
                    //取出密钥
                    var secreByte = Encoding.UTF8.GetBytes(builder.Configuration["Authentication:SecretKey"]);
                    options.TokenValidationParameters = new TokenValidationParameters()
                    {
                        //验证发布者
                        ValidateIssuer = true,
                        ValidIssuer = builder.Configuration["Authentication:Issuer"],
                        //验证接收者
                        ValidateAudience = true,
                        ValidAudience = builder.Configuration["Authentication:Audience"],
                        //验证是否过期
                        ValidateLifetime = true,

                        IssuerSigningKey = new SymmetricSecurityKey(secreByte)
                    };
                });
        }
    }
}
